package service

import (
	"teacher-helper/dao"
	"teacher-helper/models"
	"teacher-helper/models/request"
)

func AddOnePolitical(Political models.SysPolitical) error {
	return dao.DB.Create(&Political).Error
}

func GetPoliticalList(queryInfo request.QueryInfo) (Politicals []models.SysPolitical, total int, err error) {
	if queryInfo == (request.QueryInfo{}){
		err = dao.DB.Preload("Student").Find(&Politicals).Count(&total).Error
		return
	}
	if queryInfo.Query == "" {
		if err = dao.DB.Model(&models.SysPolitical{}).Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Preload("Student").Find(&Politicals).Error
	} else {
		if err = dao.DB.Model(&models.SysPolitical{}).Where("stu_id like ?", queryInfo.Query+"%").Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Where("stu_id like ?", queryInfo.Query+"%").Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Preload("Student").Find(&Politicals).Error
	}
	return
}

func UpdatePolitical(Political models.SysPolitical) error {
	return dao.DB.Save(&Political).Error
}

func DeletePolitical(id int) error {
	return dao.DB.Where("id = ?", id).Delete(&models.SysPolitical{}).Error
}



